<template>
  <div>
    <div class="tips"><img src="https://img.wifenxiao.com/h5-wfx/images/draftsBack.png" @click="Jump('supplierItems')">草稿在提交后将会删除，部分草稿删除会有延迟</div>
    <div class="goods-container">
      <div class="list-items" v-show="list.length>0">
        <van-list
            v-model="loading"
            :immediate-check="immediateCheck"
            :finished="finished"
            :error.sync="error"
            error-text="请求失败，点击重新加载"
            finished-text=""
            @load="onLoad"
          >
          <div v-for="(item, index) in list" :key="index" class="list clearfix">
            <div @click="linkTo(item.link_item)" class="img fl">
              <img :src="`${item.pic_url}480x480`" v-if="item.is_compress==1" alt />
              <img :src="`${item.pic_url}`" v-else alt />
            </div>
            <div class="g-flex fl">
              <p @click="linkTo(item.link_item)" class="title">{{item.title}}</p>
              <p>
                <span class="fz12" v-if="display_user_supplier_price==1">售价：<em>{{is_crossBorderDz? mSymbol.name:'¥'}}{{item.price}}</em>
                </span>
              </p>
              <p>
                <span class="fz12">供货价：<em>{{is_crossBorderDz? mSymbol.name:'¥'}}{{item.cost_price}}</em>
                </span>
                <s v-if="is_display_original_price==1" class="fz12">{{is_crossBorderDz? mSymbol.name:'¥'}}{{item.original_price}}</s>
              </p>
              <div>
                <!-- 是否允许供应商编辑：供应商可以对出售中、已售罄商品进行编辑操作。 -->
                <van-button class="edit-goods-btn" v-if=" is_allow_modify==1" @click="Jump('supplierAddItem',{itemId:item.item_id,draftsId: item.drafts_id})" color="#F30C23" plain hairline round type="primary">编辑</van-button>
                <van-button class="edit-goods-btn" v-else-if="!(listParams.item_status=='onsale' || listParams.item_status=='outstock')" @click="Jump('supplierAddItem',{itemId:item.item_id,draftsId: item.drafts_id})" color="#F30C23" plain hairline round type="primary">编辑</van-button>
                <van-button class="edit-goods-btn" @click="submitDrafts(item.drafts_id)" round color="linear-gradient(to right, #FE1B49, #E90104)">提交</van-button>
                <van-button class="edit-goods-btn" round @click="deleteDrafts(item.drafts_id)" >删除</van-button>

              </div>
            </div>
          </div>
        </van-list>
      </div>
      <div class="no-data" v-show="!list.length && finished">
        <no-data :noData="noData"></no-data>
      </div>
    </div>
  </div>
</template>
<script>
import { Dialog } from 'vant'
import { getSupplierItemsDrafts, deleteDrafts, submitDrafts } from '@/api/user/gys/gysApi'
import noData from '@/components/noData/index'
import { mapGetters } from 'vuex'
import Store from '@/utils/store'
export default {
  name: 'index',
  components: {
    noData
  },
  data() {
    return {
      noData: {
        img: 'https://img.wifenxiao.com/h5-wfx/images/no_order.png',
        text: '暂无数据',
        marginTop: '30px'
      },
      immediateCheck: false,
      display_user_supplier_price: '0',
      is_display_original_price: '0',
      is_allow_modify: '0',
      loading: false,
      finished: false,
      error: false,
      listParams: {
        p: 1
      },
      list: [],
      is_crossBorderDz: 0
    }
  },
  methods: {
    // 删除
    deleteDrafts(drafts_id) {
      Dialog.confirm({
        message: '确定删除该条草稿数据吗？',
        className: 'common-confirm',
        closeOnClickOverlay: true
      }).then(() => {
        this.$loadingWrap.show()
        setTimeout(() => {
          this.$loadingWrap.close()
        }, 10000)
        deleteDrafts({ drafts_id }).then(res => {
          this.$loadingWrap.close()
          if (res.status == 1) {
            this.$Error('删除成功')
            this.init()
          } else {
            this.$Error(res.msg)
          }
        })
        // on confirm
      }).catch(() => {
        // on cancel
      })
    },
    // 删除
    submitDrafts(drafts_id) {
      Dialog.confirm({
        message: '确定提交该条草稿数据吗？',
        className: 'common-confirm',
        closeOnClickOverlay: true
      }).then(() => {
        this.$loadingWrap.show()
        setTimeout(() => {
          this.$loadingWrap.close()
        }, 10000)
        submitDrafts({ drafts_id }).then(res => {
          this.$loadingWrap.close()
          if (res.status == 1) {
            this.$Error('提交成功')
            this.Jump('supplierItems')
          } else {
            this.$Error(res.msg)
          }
        })
        // on confirm
      }).catch(() => {
        // on cancel
      })
    },

    // 跳转
    Jump(url, data) {
      this.$JumpPath(this, url, data)
    },
    linkTo(link) {
      window.location.href = link
    },
    init() {
      this.$loadingWrap.show()
      setTimeout(() => {
        this.$loadingWrap.close()
      }, 50000)
      this.listParams.p = 1
      getSupplierItemsDrafts(this.listParams).then(res => {
        this.$loadingWrap.close()
        if (res.status == 1) {
          const resData = res.data
          this.display_user_supplier_price = resData.display_user_supplier_price
          this.is_display_original_price = resData.is_display_original_price
          this.is_allow_modify = resData.is_allow_modify
          this.list = resData.lists
          if (this.list <= 0) {
            this.finished = true
          }
          this.listParams.p++
        } else {
          this.$Error(res.msg)
        }
      })
    },
    onLoad() {
      getSupplierItemsDrafts(this.listParams).then(res => {
        this.loading = false
        if (res.data.lists && res.data.lists.length) {
          this.list.push(...res.data.lists)
          this.listParams.p++
        } else {
          this.finished = true
        }
      }).catch(() => {
        this.error = true
      })
    },
  },
  computed: {
    // 获取自定义符号
    ...mapGetters(['mSymbol'])
  },
  created() {
    this.init()
    // 跨境 定制
    this.is_crossBorderDz = Store.fetchLocal('isCrossBorder') || 0
  }
}
</script>

<style lang="scss">
@import "src/styles/variables";
@import "src/styles/mixin";
  .tips {
    display: flex;
    align-items: center;
    color: rgb(181, 152, 85);
    background: #f1f1f1;
    padding: 20px 40px;
    font-size: 28px;
    line-height: 40px;
    img {
      height: 34px;
      width: 34px;
      margin-right: 10px;
    }
  }
  .goods-container{
    margin:20px 0;
    .edit-goods-btn{
      display: inline-block;
      font-family: PingFang SC;
      font-size: 24px;
      height: 50px;
      line-height: 48px;
      margin-left: 10px;
      padding: 0 36px;
      border-radius:50px;
      &:first-child{
        margin-left:0;
      }
    }
    .list-items {
      background:#fff;
      .list {
        display:flex;
        padding: 20px;
        background: #fff;
        border-bottom: 1px solid #EDEDED;
        .img {
          display: inline-block;
          width: 215px;
          height: 215px;
          overflow: hidden;
          position: relative;
          border-radius: 12px;
          img {
            display:block;
            max-width: 100%;
            width: 100%;
            border-radius: 12px;
          }
          span {
            display: inline-block;
            overflow: hidden;
            position: absolute;
            top: 0px;
            left: 0px;
            color: #fff;
            background: #e4393c;
            padding: 2px 6px;
          }
        }
        .g-flex {
          flex:1;
          width: 475px;
          margin-left: 20px;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          p{
            font-family: PingFang SC;
            font-size: 24px;
            color: #333;
            line-height: 1;
            margin-bottom: 4px;
          }
          .title {
            font-weight: bold;
            @include showEllipsis;
          }
          .fz12{
            line-height: 1;
            color:#666;
            em{
              color:#F21228;
            }
            i{
              color: #FF8C01;
            }
          }
        }
      }
    }
  }
</style>